package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.article.model.SysArticleCategory;
import com.easysoft.framework.article.model.SysArticleCategoryQuery;
import com.easysoft.framework.article.model.SysArticleCategoryView;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.chat.model.SysChatroom;
import com.easysoft.framework.chat.model.SysChatroomQuery;
import com.easysoft.framework.chat.model.SysChatroomView;
import com.easysoft.framework.chat.service.SysChatroomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/8/27.
 */
@RestController
@RequestMapping(value = SystemChatRoomAction.BASE_VIEW)
public class SystemChatRoomAction {

    public static final String BASE_VIEW = "/chatRoom";
    private static final Logger logger = LoggerFactory.getLogger(SystemChatRoomAction.class);
    @Autowired
    @Qualifier("service.SysChatroomService")
    private SysChatroomService sysChatroomService;

    /**
     * 聊天室列表
     *
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "/listChatroom", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listChatroom(Page page, SysChatroomQuery query) {
        logger.info("Call SystemChatRoomAction.listChatroom({},{})", page.toString(), query.toString());
        List<SysChatroomView> listChatRoom = sysChatroomService.selectSysChatroomListByPage( query,page);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listChatRoom");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("listChatRoom", listChatRoom); // 返回查询结果集
        return mv;
    }

    /**
     * 添加聊天室
     *
     * @param sysChatroom
     * @param session
     * @return
     */
    @RequestMapping(value = "/addChatRoom", method = RequestMethod.POST)
    @SystemControllerLog(description = "添加聊天室")
    public int addSysChatRoom(SysChatroom sysChatroom, HttpSession session) {
        logger.info("Call SystemChatRoomAction.addSysChatRoom({})", sysChatroom.toString());
        sysChatroom.setSourceId((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysChatroomService.addSysChatroom(sysChatroom);
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/viewChatRoom", method = RequestMethod.POST)
    public SysChatroom viewSysChatRoom(String id) {
        logger.info("Call SystemChatRoomAction.viewSysChatRoom({})", id);
        return sysChatroomService.selectSysChatroomById(id);
    }

    /**
     *  更新聊天室
     * @param sysChatroom
     * @return
     */
    @RequestMapping(value = "/updateChatRoom", method = RequestMethod.POST)
    @SystemControllerLog(description = "更新聊天室")
    public int updateSysChatRoom(SysChatroom  sysChatroom) {
        logger.info("Call SystemChatRoomAction.updateSysChatRoom({})", sysChatroom.toString());
        return sysChatroomService.updateSysChatroom(sysChatroom);
    }

    /**
     * 删除聊天室
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteChatRoom", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除聊天室")
    public int deleteSysChatRoom(String id) {
        logger.info("Call SystemChatRoomAction.deleteSysChatRoom({})", id);
        return sysChatroomService.deleteSysChatroom(id);
    }
}
